package stud.aistar.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import stud.aistar.entity.*;
import stud.aistar.service.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2019/9/30.
 */
@Controller
@RequestMapping("/reg")
public class RegOfficeController {

    @Autowired
    private IRegOfficeService regOfficeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private ISecondDepartmentService secondDepartmentService;

    @Autowired
    private IDoctorService doctorService;

    @Autowired
    private IWorkTimeService workTimeService;

    @Autowired
    private ITimeService timeService;

    @Autowired
    private IDoctorTypeService doctorTypeService;

    @Autowired
    private IUserInfoService userInfoService;


    @GetMapping ("in")
    public  String in(){
        return "reg/regOffice";
    }


    @RequestMapping ("into")
    public String into(Model model,String fid,String sid,String did,String tid){

        List<Department> list= departmentService.findAll();

        model.addAttribute("firstdep",list);


        //一级科室

        Integer firstId= null;

        if (fid==null||"-1".equals(fid)||"".equals(fid)){
            firstId=null;
        }else {
            firstId = Integer.valueOf(fid);
            List<SecondDepartment> secList = secondDepartmentService.findByDeptId(firstId);
            model.addAttribute("seconddep",secList);
            model.addAttribute("fristId",firstId);
        }


        //二级科室
        Integer secondId=null;

        if (sid==null||"-1".equals(sid)||"".equals(sid)){
            secondId=null;
        }else {
            secondId = Integer.valueOf(sid);
            List<Doctor> docList = doctorService.findBySdeptId(secondId);
            model.addAttribute("docList",docList);
            model.addAttribute("secondId",secondId);
        }

        //医生
        Integer doctorId = null ;
        List<Time> timeList =new ArrayList<>();
        if (did==null||"-1".equals(did)||"".equals(did)){
            doctorId=null;
        }else {
            doctorId = Integer.valueOf(did);
            List<WorkTime> wList = workTimeService.findByDocId(doctorId);

            Double money = Double.valueOf(doctorTypeService.findBydocId(doctorId).getPrice());

            model.addAttribute("price",money);

            Iterator<WorkTime> iter = wList.iterator();
            while (iter.hasNext()){
                WorkTime w= iter.next();
                Time t = timeService.findById(w.getTimeId());
                timeList.add(t);
                model.addAttribute("timeList",timeList);
                model.addAttribute("doctorId",doctorId);
            }

        }
        if (tid!=null&&!tid.equals("-1")){
            model.addAttribute("tid",tid);
        }

            return "reg/doctor";

    }


//    @GetMapping("save")
//    public String save(String patient,String name,String personCard,String sex,String age,String tel,
//                       String patientId,String firstdep,String seconddep,String doctor,String time,String price){
//
//
//        Integer did = Integer.valueOf(doctor);
//        Integer tid = Integer.valueOf(time);
//        Integer count = regOfficeService.findCount(did,tid);
//        Double regPrice= Double.valueOf(price);
//
//        if (count<10){
//            RegOffice reg = new RegOffice(name,tel,personCard, Integer.valueOf(age),sex,patientId,Integer.valueOf(firstdep),
//                    Integer.valueOf(seconddep),did,tid,Integer.valueOf(patient)
//                   ,0, DateUtil.getCurrentTime(),regPrice);
//            regOfficeService.saveReg(reg);
//            return "redirect:/personal/user";
//        }else {
//            return "redirect:in";
//        }
//
//    }
    @RequestMapping("showPat")
    public String showPat(Model model, String userinfoId, HttpServletRequest request){
       User user = (User) request.getSession().getAttribute("user");

        List<Userinfo> list = userInfoService.findAllByUserId(user.getId());

        model.addAttribute("userinfoList",list);

        if (userinfoId==null){

            Userinfo userinfo = userInfoService.findByUserId(user.getId());
            model.addAttribute("uinfo",userinfo);

        }else {
            Userinfo userinfo = userInfoService.findByUserInfoId(Integer.valueOf(userinfoId));
            model.addAttribute("uinfo",userinfo);
        }

        return "reg/patient";
    }
}
